package org.deadline.model.user;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.deadline.model.config.EnumerationDataOnDemand;
import org.deadline.repository.user.UserGroupMemberRepository;
import org.deadline.service.user.UserGroupMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;

@Component
@Configurable
public class UserGroupMemberDataOnDemand {

	private Random rnd = new SecureRandom();

	private List<UserGroupMember> data;

	@Autowired
    private PersonDataOnDemand personDataOnDemand;

	@Autowired
    private EnumerationDataOnDemand enumerationDataOnDemand;

	@Autowired
    private UserGroupDataOnDemand userGroupDataOnDemand;

	@Autowired
    UserGroupMemberService userGroupMemberService;

	@Autowired
    UserGroupMemberRepository userGroupMemberRepository;

	public UserGroupMember getNewTransientUserGroupMember(int index) {
        UserGroupMember obj = new UserGroupMember();
        setCreatedBy(obj, index);
        setCreationDate(obj, index);
        setDeleted(obj, index);
        setModificationDate(obj, index);
        setPerson(obj, index);
        setUserGroup(obj, index);
        return obj;
    }

	public void setCreatedBy(UserGroupMember obj, int index) {
        Person createdBy = personDataOnDemand.getRandomPerson();
        obj.setCreatedBy(createdBy);
    }

	public void setCreationDate(UserGroupMember obj, int index) {
        Date creationDate = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setCreationDate(creationDate);
    }

	public void setDeleted(UserGroupMember obj, int index) {
        Boolean deleted = true;
        obj.setDeleted(deleted);
    }

	public void setModificationDate(UserGroupMember obj, int index) {
        Date modificationDate = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setModificationDate(modificationDate);
    }

	public void setPerson(UserGroupMember obj, int index) {
        Person person = personDataOnDemand.getRandomPerson();
        obj.setPerson(person);
    }

	public void setUserGroup(UserGroupMember obj, int index) {
        UserGroup userGroup = userGroupDataOnDemand.getRandomUserGroup();
        obj.setUserGroup(userGroup);
    }

	public UserGroupMember getSpecificUserGroupMember(int index) {
        init();
        if (index < 0) {
            index = 0;
        }
        if (index > (data.size() - 1)) {
            index = data.size() - 1;
        }
        UserGroupMember obj = data.get(index);
        Long id = obj.getId();
        return userGroupMemberService.findUserGroupMember(id);
    }

	public UserGroupMember getRandomUserGroupMember() {
        init();
        UserGroupMember obj = data.get(rnd.nextInt(data.size()));
        Long id = obj.getId();
        return userGroupMemberService.findUserGroupMember(id);
    }

	public boolean modifyUserGroupMember(UserGroupMember obj) {
        return false;
    }

	public void init() {
        int from = 0;
        int to = 10;
        data = userGroupMemberService.findUserGroupMemberEntries(from, to);
        if (data == null) {
            throw new IllegalStateException("Find entries implementation for 'UserGroupMember' illegally returned null");
        }
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<UserGroupMember>();
        for (int i = 0; i < 10; i++) {
            UserGroupMember obj = getNewTransientUserGroupMember(i);
            try {
                userGroupMemberService.saveUserGroupMember(obj);
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {
                    ConstraintViolation<?> cv = iter.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            userGroupMemberRepository.flush();
            data.add(obj);
        }
    }
}
